﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for SearchBox.xaml
    /// </summary>
    public partial class SearchBox : Window
    {
        private TagAutoCompleteTextBox tagBox;

        public SearchBox(MainWindow window)
        {
            this.Owner = window;

            InitializeComponent();

            Dictionary<string, int> tagsSummary = Database.GetTagsSummary(ImageManager.Connection);
            this.tagBox = new TagAutoCompleteTextBox {
                Margin = new Thickness(23, 10, 22, 0),
                TagsSummary = tagsSummary,
                AutoCompleteEnabled = ImageManager.SearchTags,
                IgnoreDashPrefix = true};
            this.rootGrid.Children.Add(this.tagBox);
            
            //this.tagBox.TagsSummary = tagsSummary;
            this.tagBox.AutoCompleteEnabled = ImageManager.SearchTags;

            string currentSearch = ImageQuery.Search;
            this.tagBox.TextBox.Text = currentSearch;
            this.tagBox.TextBox.SelectAll();

            this.searchFileName.IsChecked = ImageManager.SearchFileName;

            this.searchTags.IsChecked = ImageManager.SearchTags;

            this.tagBox.TextBox.Focus();
        }

        private void searchOk_Click(object sender, RoutedEventArgs e)
        {
            QueryOperations.RunSearch(this.tagBox.TextBox.Text.Trim(), (bool)this.searchFileName.IsChecked, (bool)this.searchTags.IsChecked);

            this.DialogResult = true;
        }

        private void searchTags_Click(object sender, RoutedEventArgs e)
        {
            this.tagBox.AutoCompleteEnabled = (bool)this.searchTags.IsChecked;
        }
    }
}
